package cn.tedu.knows.portal.controller;


import cn.tedu.knows.portal.exception.ServiceException;
import cn.tedu.knows.portal.model.Question;
import cn.tedu.knows.portal.service.IQuestionService;
import cn.tedu.knows.portal.service.impl.UserDetailsServiceImpl;
import cn.tedu.knows.portal.vo.QuestionVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2021-10-27
 */
@RestController
@RequestMapping("/v1/questions")
@Slf4j
public class QuestionController {

    @Autowired
    private IQuestionService questionService;
    @GetMapping("/my")
    //@AuthenticationPrincipal 注解能够将当前Spring - Security中
    //保存的登录用户的信息取出保存到参数UserDetails 中
    public PageInfo<Question> my(@AuthenticationPrincipal UserDetails user,Integer pageNum ){
        Integer pageSize=8;
        if (pageNum==null){
            pageNum=1;
        }
        PageInfo<Question> pageInfo = questionService.getMyQuestion(user.getUsername()
        ,pageNum,pageSize);
        return pageInfo;
    }
    @GetMapping("/hot")
    public List<Question> findHotQuestions(){
        return questionService.findHotQuestions();
    }

    @PostMapping("")
    public String createQuestion(@Validated QuestionVo questionVo,
                                 BindingResult result,
                                 @AuthenticationPrincipal UserDetails user){
        log.debug("接受到表单信息:"+questionVo);
        if (result.hasErrors()){
            String msg = result.getFieldError().getDefaultMessage();
            return msg;
        }
        //调用业务逻辑层方法

            questionService.saveQuestion(questionVo,user.getUsername());
            return "ok";
    }

    @GetMapping("/teacher")
    //当前登录用户在Spring-Security登录时
    //必须拥有/question/answer权限才可以访问以下的方法
    @PreAuthorize("hasAuthority('/question/answer')")
    public PageInfo<Question> teacher(@AuthenticationPrincipal UserDetails user,Integer pageNum){
        Integer pageSize = 8;
        if (pageNum==null) {
            pageNum = 1;
        }
        //调用业务逻辑层
        PageInfo<Question> pageInfo  = questionService
                .getTeacherQuestions(user.getUsername(),pageNum,pageSize);
        return pageInfo;
    }
    //根据id查询问题详情 SpringMvc支持我们编写匹配占位符url
    // {id} 就是占位符 在整个控制器方法中,没有精确匹配时会启动匹配
    //.v1/question/150 这个路径中150会匹配给id
    //方法参数利用特殊注解可以获得150这个值,在方法中使用
    @GetMapping("/{id}")          //要想获得{id}匹配的值
                                    // 1,必须编写@PathVariable开头
                                    // 2,参数名称必须和{}里面的名称一致
    public Question getQuestion(@PathVariable Integer id){
      Question question =  questionService.getQuestionById(id);
        return question;
    }

}
